package edu.ccut.titan.tag.report.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.titan.tag.report.design.ReportDesignInfo;

public class ReportTagInfo {
	public String title;

	public List<Map<String, Object>> source;

	public ReportDesignInfo designInfo;

	public ReportTagInfo() {
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Map<String, Object>> getSource() {
		return source;
	}

	public void setSource(List<Map<String, Object>> source) {
		this.source = source;
	}

	public ReportDesignInfo getDesignInfo() {
		return designInfo;
	}

	public void setDesignInfo(ReportDesignInfo designInfo) {
		this.designInfo = designInfo;
	}

	public void sendRequest(HttpServletRequest request,
			HttpServletResponse response, String type,
			Map<String, String> OtherParameter) {
		Map<String, String> fillParameter = getFillParameter(OtherParameter);
		request.setAttribute(IReport.SOURCE, this.source);
		request.getSession().setAttribute(IReport.SOURCE, this.source);
		request.setAttribute(IReport.TYPE, type);
		request.setAttribute(IReport.DYNAMIC_DESIGN, this.designInfo);
		request.getSession().setAttribute(IReport.DYNAMIC_DESIGN,
				this.designInfo);
		request.setAttribute(IReport.PARAMETER, fillParameter);
		request.getSession().setAttribute(IReport.PARAMETER, fillParameter);
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("dd.report");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Map<String, String> getFillParameter(
			Map<String, String> OtherParameter) {
		Map<String, String> fillParameter = new HashMap<String, String>();
		fillParameter.put("title", this.getTitle());
		if (OtherParameter != null) {
			Set<String> keys = OtherParameter.keySet();
			Iterator<String> ir = keys.iterator();
			while (ir.hasNext()) {
				String key = ir.next();
				String value = OtherParameter.get(key);
				fillParameter.put(key, value);
			}
		}
		return fillParameter;
	}
}
